package com.xianxing.search.service.impl;

import com.xianxing.article.feign.ArticleFeign;
import com.xianxing.common.pojo.Article;
import com.xianxing.common.util.Response;
import com.xianxing.search.pojo.ArticleInfo;
import com.xianxing.search.repositories.ArticleInfoRepository;
import com.xianxing.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate esTemplate;

    @Autowired
    private ArticleInfoRepository articleInfoRepository;

    @Autowired
    private ArticleFeign articleFeign;


    @Override
    public void upDataByArticleId(String spuId) {


    }

    @Override
    public void delDataByArticleId(String spuId) {

    }

    @Override
    public void create() {
        esTemplate.createIndex(ArticleInfo.class);
        esTemplate.putMapping(ArticleInfo.class);

    }

    @Override
    public void importAll() {
        Response response = articleFeign.findAll();
        List<Article> articleList = (List<Article>) response.getData();

        List<ArticleInfo> articleInfoList = articleList.stream().map(article -> {
            ArticleInfo articleInfo = new ArticleInfo();
            articleInfo.setArticleId(article.getArticleId());
            articleInfo.setArticleName(article.getArticleName());
            articleInfo.setArticleContent(article.getArticleContent());
            articleInfo.setArticleTime(article.getArticleTime());
            articleInfo.setArticleClick(article.getArticleClick());
            articleInfo.setTypeId(article.getTypeId());
            articleInfo.setUserName("admin");
            return articleInfo;
        }).collect(Collectors.toList());
        articleInfoRepository.saveAll(articleInfoList);
    }
}
